package sg.edu.nus.iss.phoenix.presenterproducer.controller;

import java.io.*;
import java.util.*;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import sg.edu.nus.iss.phoenix.presenterproducer.delegate.ReviewSelectPresenterProducerDelegate;
import sg.edu.nus.iss.phoenix.presenterproducer.entity.PresenterProducerSearchObject;
import sg.edu.nus.iss.phoenix.authenticate.entity.User;
import sg.edu.nus.iss.phoenix.core.controller.FCUtilities;

@WebServlet("/PresenterProducerController/*")
public class PresenterProducerController extends HttpServlet{
	  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
     * Handle get requests by forwarding to a common processor
     */
    protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
    	process(request,response);
    }
    
    /**
     * Handle post requests by forwarding to a common processor
     */
    protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
    	process(request,response);
    }
    
    protected void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    	// the default next page
        String selection = FCUtilities.stripPath(request.getPathInfo());
        ReviewSelectPresenterProducerDelegate reviewselectPPdelegate = new ReviewSelectPresenterProducerDelegate();
        PresenterProducerSearchObject ppsObj = new PresenterProducerSearchObject();
        ArrayList<User> data = new ArrayList<User>();
        
        //ppsObj.setuserRollName(request.getParameter("UserRole"));
    	//ppsObj.setrollid(Integer.parseInt(request.getParameter("roleId")));
        
        if(request.getParameter("roleId")!=null)
        {
        	ppsObj.setrollid(Integer.parseInt(request.getParameter("roleId"))); 
        }
        else
        {
        	ppsObj.setrollid(Integer.parseInt("2"));
        }
        
        if (ppsObj.getrollid()==2){
        	ppsObj.setuserRollName("Presenter");
        } else {
        	ppsObj.setuserRollName("Producer");
        }
        	
        
    	ppsObj.setuserRollName("Presenter");
    	
    	request.setAttribute("roleId", "2");
        switch (selection) {
        
        case "load":
        	if (ppsObj.getuserRollName()=="Presenter") {
        		data = reviewselectPPdelegate.searchAllPresenter(ppsObj);
        	} else if (ppsObj.getuserRollName()=="Producer") {
        		data = reviewselectPPdelegate.searchAllProducer(ppsObj);
        	}
        	request.setAttribute("searchppdata", data);
        	break;
        case "search":
        	//if (request.getParameter("UserRole")=="Presenter")
        	if (ppsObj.getuserRollName()=="Presenter") {
        		
        		if (request.getParameter("name") != null) {
            		ppsObj.setname(request.getParameter("name"));
            		data = reviewselectPPdelegate.searchPresenter(ppsObj);
            		System.out.println("Search Presenter :: searchPresenter");
            	} else {
            		data = reviewselectPPdelegate.searchAllPresenter(ppsObj);
            	} 
        		//if (request.getParameter("UserRole")=="Producer")
        	} else if (ppsObj.getuserRollName()=="Producer") {
        		if (request.getParameter("name") != null) {
            		ppsObj.setname(request.getParameter("name"));
            		data = reviewselectPPdelegate.searchProducer(ppsObj);
            	} else {
            		data = reviewselectPPdelegate.searchAllProducer(ppsObj);
            	}
        	}
        	
        	//if searching user name is not found then display the error message and load all the user details in page.
        	if (data.size()==0) {
        		request.setAttribute("ppnotFound","Presenter/producer '" + request.getParameter("name") + "' is not found in the list.");
        		if (ppsObj.getuserRollName()=="Presenter") {
        			data = reviewselectPPdelegate.searchAllPresenter(ppsObj);
        		} else if (ppsObj.getuserRollName()=="Producer") {
        			data = reviewselectPPdelegate.searchAllProducer(ppsObj);
        		}
        	}
        	request.setAttribute("searchppdata", data);
        	break;
        default:
        	
        	if (ppsObj.getuserRollName()=="Presenter") {
        		data = reviewselectPPdelegate.searchAllPresenter(ppsObj);
        	} else if (ppsObj.getuserRollName()=="Producer") {
        		data = reviewselectPPdelegate.searchAllProducer(ppsObj);
        	}
        	request.setAttribute("searchppdata", data);
        	
			break;
        }
        
            
       RequestDispatcher rd = request.getRequestDispatcher("/pages/presenterProducer.jsp");
       rd.forward(request,response);
    }

}
